package com.yuandian.api.customer_perception.po;

import com.yuandian.po.BasePageSearch;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明: 告警周期柱状图
 *
 * @Date 2021/8/20 13:40
 * @Author User
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class HistoGramParams extends BasePageSearch {

    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 系统id
     */
    private Integer systemId;

    /**
     * 业务编码
     */
    private String businessCode;

    /**
     * 告警类型(告警指标id)
     */
    private Integer alarmIndicatorId;

    /**
     * 故障类型(告警返回码)
     */
    private String alarmResultCode;

    /**
     * 统计类型
     */
    private String statType;

    private String statValue;

    /**
     * 失败率大于
     */
    private Double gtFailRate;

    /**
     * 查询时间
     */
    private List<String> queryTimeList;

    public void handleQueryTime () {
        queryTimeList = new ArrayList<>();

//        LocalDate nowDay = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.parse(getEndTime(), dateTimeFormatter);
        LocalDate nowDay = localDateTime.toLocalDate();
        // 连续 7 天
        if ("1".equals(statType)) {
            for (int i = 7; i > 0; i--) {
                LocalDate tmp = nowDay.minusDays(i);
                queryTimeList.add(dtf.format(tmp));
            }
        }
        // 连续四周
        else if ("2".equals(statType) || "连续四周产生告警".equals(statValue)) {
            for (int i = 28; i > 0; i--) {
                LocalDate tmp = nowDay.minusDays(i);
                queryTimeList.add(dtf.format(tmp));
            }
        }
        // 连续四周每周几
        else {
            LocalDate now = LocalDate.now();
            int weekDay = now.getDayOfWeek().getValue();
            int queryDay = getQueryDay(statValue);

            int endPoint;
            if (weekDay > queryDay) {
                endPoint = weekDay - queryDay;
            } else {
                endPoint = (7 - queryDay) + weekDay;
            }

            int index = 4;
            while (index > 0) {
                LocalDate tmp = now.minusDays(endPoint);
                queryTimeList.add(dtf.format(tmp));
                endPoint += 7;
                index--;
            }
        }
    }


    public static int getQueryDay (String statValue) {
        if ("连续四周每周1产生告警".equals(statValue)) {
            return 1;
        }
        if ("连续四周每周2产生告警".equals(statValue)) {
            return 2;
        }
        if ("连续四周每周3产生告警".equals(statValue)) {
            return 3;
        }
        if ("连续四周每周4产生告警".equals(statValue)) {
            return 4;
        }
        if ("连续四周每周5产生告警".equals(statValue)) {
            return 5;
        }
        if ("连续四周每周6产生告警".equals(statValue)) {
            return 6;
        }
        if ("连续四周每周日产生告警".equals(statValue)) {
            return 7;
        }
        return -1;
    }
}
